package com.woniu.mall.controller;

import com.alipay.api.response.AlipayTradeQueryResponse;
import com.woniu.common.annotation.LoginUser;
import com.woniu.common.entity.Result;
import com.woniu.mall.DTO.MallOrderDTO;
import com.woniu.mall.DTO.MallProductDTO;
import com.woniu.mall.VO.CartVO;
import com.woniu.mall.VO.CouponVO;
import com.woniu.mall.pojo.MallOrder;
import com.woniu.mall.service.IMallOrderService;
import com.woniu.user.constant.UserRedisKey;
import com.woniu.user.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 坤
 * @since 2023-05-22
 */
@RestController
@RequestMapping("/mallOrder")
@Api(tags = "订单管理")
public class MallOrderController {

    @Autowired
    private IMallOrderService mallOrderService;


    @ApiOperation("根据订单号查询支付宝支付状态")
    @PostMapping("/checkPayStatus/{outTradeNo}")
    public Result checkPayStatus(@PathVariable String outTradeNo) {
        AlipayTradeQueryResponse payStatus = mallOrderService.checkPayStatus(outTradeNo);
        return Result.ok(payStatus);
    }


    @ApiOperation("根据订单号获取订单信息")
    @PostMapping("/getOrderById/{outTradeNo}")
    public Result getOrderById(@PathVariable String outTradeNo) {
        MallOrder order = mallOrderService.getOrderById(outTradeNo);
        return Result.ok(order);
    }

    @ApiOperation("添加订单信息")
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody MallOrder order) {
        Integer result = mallOrderService.addOrder(order);
        String info = result > 0 ? "订单创建成功" : "订单创建失败";
        return Result.ok(info);
    }

    @ApiOperation("修改订单信息")
    @PostMapping("/updateOrder")
    public Result updateOrder(@RequestBody MallOrder order) {
        Integer result = mallOrderService.updateOrder(order);
        String info = result > 0 ? "订单修改成功" : "订单修改失败";
        return Result.ok(info);
    }

    @ApiOperation("查询购物车")
    @PostMapping("/selectCart")
    public Result<Map> selectCart(@LoginUser UserLoginVO userLoginVO) {
        Map cartVOMap = mallOrderService.selectCart(userLoginVO);
        return Result.ok(cartVOMap);
    }

    @ApiOperation("加入购物车")
    @PostMapping("/addCart")
    public Result addCart(@RequestBody MallProductDTO mallProductDTO, @LoginUser UserLoginVO userLoginVO) {

        Boolean flag = mallOrderService.addCart(mallProductDTO, userLoginVO);
        return Result.ok(flag);
    }

    @ApiOperation("删除购物车")
    @GetMapping("/delectCart")
    public Result delectCart(@LoginUser UserLoginVO userLoginVO) {

        Boolean flag = mallOrderService.deleteCart(userLoginVO);
        return Result.ok(flag);
    }

    @ApiOperation("查询结算商品")
    @PostMapping("/settlement")
    public Result<CartVO> settlement(@RequestBody List<MallProductDTO> mallProductDTOList, @LoginUser UserLoginVO userLoginVO) {

        CartVO cartVO = mallOrderService.settlement(mallProductDTOList, userLoginVO.getMobile());
        return Result.ok(cartVO);
    }

    @ApiOperation("查询可用与不可用优惠券")
    @PostMapping("/selectCoupon")
    public Result<CouponVO> selectCoupon(@RequestBody List<MallProductDTO> mallProductDTOList, @LoginUser UserLoginVO userLoginVO) {
        CouponVO couponVO = mallOrderService.selectCoupon(mallProductDTOList, userLoginVO.getMobile());
        return Result.ok(couponVO);
    }

    /*@ApiOperation("保证订单幂等性token")
    @PostMapping("/creatToken")
    public Result createToken(@LoginUser UserLoginVO userLoginVO) {

        UUID uuid = UUID.randomUUID();
        String token = uuid.toString().replaceAll("-", "");
        redisTemplate.boundValueOps(UserRedisKey.CREATEORDERLICENSE + userLoginVO.getMobile()).set(token);
        return Result.ok();
    }*/

    @ApiOperation("创建本地商城购买订单")
    @PostMapping("/createOrder")
    public Result createOrder(@RequestBody MallOrderDTO mallOrderDTO, @LoginUser UserLoginVO userLoginVO) {
        /*Boolean flag = redisTemplate.delete(UserRedisKey.CREATEORDERLICENSE + userLoginVO.getMobile());
        if (!flag){
            return Result.error("交易已存在");
        }*/
        MallOrder order = mallOrderService.createOrder(mallOrderDTO, userLoginVO);
        return Result.ok(order);
    }

    @ApiOperation("同步库存")
    @GetMapping("/saveStock")
    public Result saveStock(){
        mallOrderService.saveStock();
        return Result.ok();
    }
}
